package com.wondertek.poms.service.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.constant.CurrentUser;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IEquityTreeService;
import com.wondertek.poms.core.service.IEquityNodeService;
import com.wondertek.poms.dao.po.EquityNode;
import com.wondertek.poms.dao.po.EquityTree;
import com.wondertek.poms.dao.pojo.EquityNodePojo;
import com.wondertek.poms.service.vo.EquityTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Api(value = "EquityTreeController", description = "权益树管理")
@RequestMapping(value="/equityTree")
@RestController
public class EquityTreeController {

    @Autowired
    private IEquityTreeService equityTreeService;
    @Autowired
    private IEquityNodeService equityNodeService;

    @ApiOperation("权益树管理 列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ResultBean list(@RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "20") int rows,
                           @RequestParam(defaultValue = "createTime") String sidx,
                           @RequestParam(defaultValue = "desc") String sord){
        Page<EquityTree> list=equityTreeService.findAll(page,rows,sord,sidx);
        return PageBean.ok(list.getTotalPages(),list.getTotalElements(),list.getContent());
    }

    @ApiOperation("权益树管理 添加")
    @PostMapping(value = "/add")
    @ResponseBody
    public ResultBean add(@RequestBody EquityTreeVo equityTreeVo){
        List<String> messageList = ValidateFilter.getFilterMessage(equityTreeVo, Constants.VALIDATE_DEFAULT);
        if (!messageList.isEmpty()) {
            return ResultBean.error(messageList.toString());
        }
        if(isRepeat(equityTreeVo)){
            return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
        }

        EquityTree equityTree=new EquityTree();
        BeanUtils.copyProperties(equityTreeVo,equityTree);
        equityTree.setEquityTreeDepth(0);
        equityTree.setCreateTime(new Date());
        equityTree.setAuthor(CurrentUser.currUserName());
        if(equityTreeVo.getAutoPublish()==0){
            equityTree.setActiveStatus("2");
        }else if(equityTreeVo.getAutoPublish()==1){
            //todo  权益树发布
            equityTree.setActiveStatus("0");
        }
        EquityTree result=equityTreeService.save(equityTree);
        return ResultBean.ok(result);
    }

    @ApiOperation("权益树管理 删除")
    @DeleteMapping(value = "/delete")
    @ResponseBody
    public ResultBean delete(String ids){
        if (StrUtil.isBlank(ids)) {
            return ResultBean.error(MessageCodes.get(MessageCodes.M980203));
        }
        String[] idArray = ids.split(",|，");
        String errIds="";
        for(String id:idArray){
            EquityTree equityTree=equityTreeService.get(Convert.toLong(id));
            if("0".equals(equityTree.getActiveStatus())){
                errIds+=","+id;
            }
        }
        if(errIds.isEmpty()){
            for(String id:idArray){
                equityTreeService.deleteById(Convert.toLong(id));
            }
        }else{
            errIds=errIds.substring(1);
            String msg="id为"+errIds+"的权益树已发布，不能删除";
            return ResultBean.error(msg);
        }
        return ResultBean.ok();
    }


    @ApiOperation("通过权益树id获取全部节点")
    @GetMapping(value = "/getEquityNodeListByTreeId")
    @ResponseBody
    public ResultBean getEquityNodeListByTreeId (String id){
        if (StrUtil.isBlank(id)) {
            return ResultBean.error(MessageCodes.get(MessageCodes.M980203));
        }
        EquityNodePojo equityNodePojo=equityTreeService.getAllEquityNodesByTreeId(Convert.toLong(id));
        return ResultBean.ok(equityNodePojo);
    }

    @ApiOperation("通过子节点id获取子节点信息")
    @GetMapping(value = "/getEquityNodeByNodeId")
    @ResponseBody
    public ResultBean getEquityNodeByNodeId(String id){
        if (StrUtil.isBlank(id)) {
            return ResultBean.error(MessageCodes.get(MessageCodes.M980203));
        }
        EquityNode equityNode=equityNodeService.get(Convert.toLong(id));
        return ResultBean.ok(equityNode);
    }

    public boolean isRepeat(EquityTreeVo equityTreeVo){
        EquityTree temp=new EquityTree();
        temp.setEquityTreeName(equityTreeVo.getEquityTreeName());
        Optional<EquityTree> equityTreeOptional=equityTreeService.findOne(Example.of(temp));
        if(equityTreeOptional.isPresent()){
            EquityTree equityTree=equityTreeOptional.get();
            if(equityTreeVo.getId()!=null && equityTreeVo.getId()==equityTree.getId()){
                return true;
            }else if(equityTreeVo.getId()==null){
                return true;
            }
        }
        return false;
    }
}
